<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="public/css/style_ontap.css" type="text/css">
<?php
require_once 'model/dbs.php';
?>


<?php
$query = "SELECT * FROM companies";
$companies = getData($query);
$querya = "SELECT * FROM apps";
$apps = getData($querya);
?>
<script type ="text/javascrip"> 
    alert ("a");
</script>
<div id="form">
    <p class="tieude"><h1>Hà Văn Lộc</h1></p>
<form method="POST">
        <table>
            <tr><td> Min fans</td><td><input type="text" name="min_fans"></td></tr></br>
            <tr><td> Max fans</td><td><input type="text" name="max_fans"></td></tr></br>
            <tr><td> Keyword</td><td> <input type="text" name="keyword"></td></tr></br>
            <tr><td> Company</td><td><select name="id_company">
              
                <?php foreach ($companies as $company): ?>

                    <option value="<?php echo $company['id'] ?>"><?php echo $company['name'] ?></option>
                <?php endforeach; ?>
            </select></td></tr></br>
            <tr><td> Apps</td><td><select name="id_apps">
                
                <?php foreach ($apps as $app): ?>

                    <option value="<?php echo $app['id'] ?>"><?php echo $app['name'] ?></option>
                <?php endforeach; ?>
            </select></td></tr></br>
            
        </table>
    <input type="submit" name="submit" value="Search" >
    </form>
</div>
<?php if (isset($_POST['submit'])): ?>
          
    <?php 
   
        $min_fans = $_POST['min_fans'];
        $max_fans = $_POST['max_fans'];
        $keyword = $_POST['keyword'];
        $company=$_POST["id_company"];
        $apps=$_POST["id_apps"];
        if(!empty($company))
            $company="companies.id='$company' AND";
        else
           $company="";
        if(!empty($apps))
            $apps="apps.id='$apps' AND";
        else
           $apps="";
        if(empty($max_fans)&& !empty($min_fans))
        {
          $query="SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  'pages.likes>= $min_fans' and
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,10";
        }
        else if(!empty($max_fans)&& empty($min_fans))
        {
          $query="SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  'pages.likes<= $max_fans' and
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,10";
        }
        else if(empty($max_fans)&& empty($min_fans))
        {
           $query="SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,10";
        }
        else
        {  
        $query="SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  'pages.likes>= $min_fans' and
                  'pages.likes<= $max_fans' and
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,10";
        }
        var_dump($query);
        $pages = getData($query);     
    ?>
    
    
    
<?php else: ?> 
    <?php
    $query = "SELECT * FROM pages LIMIT 0,15";
    $pages = getData($query);
    ?>
<?php endif; ?>
<?php if (!empty($pages)): ?>

    <table id="bang" border="1px">
        <tr class="dong1">
            <td> STT </td>
            <td> Id </td>               
            <td> Name </td>
            <td> Picture </td>
            <td> likes </td>
            <td> Talking </td>
            <td> category </td>
            <td> link </td>
            <td> website </td>
            <td> description </td>
        </tr>
        <?php foreach ($pages as $page): ?>
            <tr class="dong2">
                <td><?php echo $page["pid"] ?></td>
                <td><?php echo $page["id"] ?> </td>                    
                <td><?php echo $page["name"] ?></td>
                <td><img src="<?php echo $page['picture'] ?>"></td>
                <td><?php echo $page['likes'] ?></td>
                <td><?php echo $page["talking_about_count"] ?></td>
                <td><?php echo $page["category"] ?></td>
                <td><a href='<?php echo $page["link"] ?>'><?php echo $page["link"] ?></td>
                <td><a href='<?php echo $page["website"] ?>'><?php echo $page["website"] ?></td>
                <td><?php echo substr($page['description'], 0, 100) ?></td>
            </tr>
        <?php endforeach; ?>
    </table>
<?php endif; ?>


